# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 结构化数据-Pydantic 类.py
# @Author: dongguangwen
# @Date  : 2025-03-23 18:27
from typing import Optional
from langchain_openai import ChatOpenAI
from pydantic import BaseModel, Field

model = ChatOpenAI(model="Pro-128K", api_key="dzCDkUHufCZEsuWlfVQd:",
                   base_url="https://spark-api-open.xf-yun.com/v1")


# Pydantic
class Joke(BaseModel):
    """Joke to tell user."""

    setup: str = Field(description="The setup of the joke")
    punchline: str = Field(description="The punchline to the joke")
    rating: Optional[int] = Field(
        default=None, description="How funny the joke is, from 1 to 10"
    )


structured_llm = model.with_structured_output(Joke)
print(structured_llm)

ret = structured_llm.invoke("Tell me a joke about cats")
print(ret)
